package com.jiaming.employee.attendance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiaming.employee.attendance.bean.pojo.Attendance;
import com.jiaming.employee.attendance.bean.vo.AddAttVO;
import com.jiaming.employee.attendance.bean.vo.DayVO;
import com.jiaming.employee.attendance.bean.vo.PageAttDataVO;
import com.jiaming.employee.attendance.bean.vo.PageAttVO;
import com.jiaming.employee.attendance.mapper.AttendanceMapper;
import com.jiaming.employee.attendance.service.IAttendanceService;
import com.jiaming.employee.common.bean.vo.MyPageVO;
import com.jiaming.employee.department.bean.pojo.Department;
import com.jiaming.employee.department.service.IDepartmentService;
import com.jiaming.employee.emp.bean.pojo.Emp;
import com.jiaming.employee.emp.service.IEmpService;
import com.jiaming.employee.salary.service.ISalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sun
 * @create 2021-05-15-9:51
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance>
        implements IAttendanceService {

    @Autowired
    IEmpService empService;

    @Autowired
    IDepartmentService departmentService;

    @Autowired
    ISalaryService salaryService;

    @Override
    public MyPageVO<PageAttDataVO> pageAtt(PageAttVO pageAttVO) {

        Page<Attendance> page = new Page<>();
        page.setSize(pageAttVO.getPageSize());
        page.setCurrent(pageAttVO.getPageNum());
        QueryWrapper<Attendance> queryWrapper = new QueryWrapper<>();
        if (pageAttVO.getName() != null) {

            queryWrapper.eq("emp_name",pageAttVO.getName());
        }
        if (pageAttVO.getName() != null) {

            queryWrapper.eq("job",pageAttVO.getJob());
        }
        queryWrapper.orderByDesc("id");
        this.page(page,queryWrapper);
        List<Attendance> records = page.getRecords();
        List<PageAttDataVO> dataVOS = new ArrayList<>();
        for (Attendance attendance : records) {
            PageAttDataVO pageAttDataVO = new PageAttDataVO();
            Emp emp = empService.getById(attendance.getEmpId());
            Department dep = departmentService.getById(attendance.getDepId());
            pageAttDataVO.setEmpName(emp.getName());
            pageAttDataVO.setJob(emp.getJob());
            pageAttDataVO.setDepName(dep.getName());
            pageAttDataVO.setStartTime(attendance.getStartTime());
            pageAttDataVO.setEndTime(attendance.getEndTime());
            dataVOS.add(pageAttDataVO);
        }
        MyPageVO<PageAttDataVO> data = new MyPageVO<>();
        data.setItems(dataVOS);
        data.setPageNum(page.getCurrent());
        data.setPageSize(page.getSize());
        data.setTotalItems(page.getTotal());
        data.setTotalPages(page.getPages());
        return data;
    }

    @Override
    public Attendance addAtt(AddAttVO addAttVO) {
        Emp emp = empService.getById(addAttVO.getEmpId());
        Attendance attendance = new Attendance();
        attendance.setDepId(emp.getDepId());
        attendance.setEmpId(emp.getId());
        attendance.setStartTime(addAttVO.getStartTime());
        attendance.setEndTime(addAttVO.getEndTime());
        this.save(attendance);
        return attendance;
    }

    @Override
    public void countDay() {
        List<DayVO> dayVO = this.baseMapper.countDay();
        for (DayVO vo : dayVO) {
            salaryService.updateByEmpId(vo);
        }
    }
}
